package com.inspur.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.mybatisplus.entity.User;
import com.inspur.mybatisplus.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping(value = "test")
    public String test(){
        // 插入
        User user = new User();
        user.setId(10L);
        userMapper.insert(user);

        // 修改
        user.setEmail("12345y@13.dco");
        user.setName("Malcolm");
        userMapper.updateById(user);


        // 查询-根据id
        userMapper.selectById(1);

        // 查询-根据map条件
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("name","Sandy");//写表中的列名
        columnMap.put("age","21");
        userMapper.selectByMap(columnMap);

        // 查询-条件
        userMapper.selectList(new QueryWrapper<User>().like("name","Malcolm").eq("age",24).last("order by id"));

        // 批量查询-根据ids
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(2);
        idList.add(3);
        userMapper.selectBatchIds(idList);


        // 分页查询
        IPage<User> userList=userMapper.selectPage(
                new Page<User>(1, 10),
                new QueryWrapper<User>().like("name","1")
        );

        // 删除
        userMapper.deleteById(1);
//        userMapper.delete(null);
//        userMapper.deleteBatchIds(idList);
//        userMapper.deleteByMap(columnMap);




        return "please look at db.";
    }
}
